﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Facebook;

namespace FeedBack {
    public static class JsonExtensions {
        public static JsonObject Get(this JsonObject target, string key) {
            if (target == null || !target.Keys.Contains(key)) {
                return null;
            }
            return target[key] as JsonObject;
        }

        public static T Get<T>(this JsonObject target, string key) {
            if (target == null || !target.Keys.Contains(key)) {
                return default(T);
            }
            return (T)target[key];
        }

        public static IEnumerable<JsonObject> GetArray(this JsonObject target, string key) {
            if (target == null || !target.Keys.Contains(key)) {
                return null;
            }
            return SafeGetArray(target, key);
        }

        private static IEnumerable<JsonObject> SafeGetArray(this JsonObject target, string key) {
            foreach (var item in target[key] as JsonArray) {
                yield return item as JsonObject;
            }
        }
    }
}
